package org.fpp.jdk21study;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author bigbird-0101
 * @date 2024-02-20 11:31
 */
public class RecordPatternStudy {
    static class RecordPattern{
        record A(int x,int y){

        }

        public static void main(String[] args) {
            var a=new A(1,2);
            test(a);
            testPattern(a);
        }

        public static void test(Object o){
            if(o instanceof A a){
                System.out.println(a);
            }
        }

        public static void testPattern(Object o){
            if(o instanceof A(int x,int y)){
                System.out.println(x+":::"+y);
            }
        }
    }


    static class RecordPattern2{
        record B(String a,String b){

        }
        record A(B b,int y){

        }

        public static void main(String[] args) {
            var a=new A(new B("a","d"),2);
            test(a);
            testPattern(a);
        }

        public static void test(Object o){
            if(o instanceof A a){
                System.out.println(a);
            }
        }

        public static void testPattern(Object o){
            if(o instanceof A(B(String a,String c),int y)){
                System.out.println(a+"::"+c+":::"+y);
            }
        }
    }

    static class RecordPattern3{
        private static final Logger logger = Logger.getLogger("123");

        static class SuperB{
            private String a;
            private String b;
            public SuperB(String a,String b){
                this.a=a;
                this.b=b;
            }

            public String getA() {
                return a;
            }

            public void setA(String a) {
                this.a = a;
            }

            public String getB() {
                return b;
            }

            public void setB(String b) {
                this.b = b;
            }

            @Override
            public String toString() {
                return "SuperB{" +
                        "a='" + a + '\'' +
                        ", b='" + b + '\'' +
                        '}';
            }
        }
        static class B extends SuperB{

            public B(String a, String b) {
                super(a, b);
            }
        }

        static class D extends B{
            public D(String a, String b) {
                super(a, b);
            }
        }
        record C(String a,String b){

        }
        record A<T>(T b, T y){

        }

        public static void main(String[] args) {
            var a=new A<>(new B("123","123"),new B("123","1234"));
            testPattern(a);
            testPattern2(a);
            logger.log(Level.INFO,"abcd {}","ddddd");
        }



        public static void testPattern(A<B> o){
            switch (o){
                case A<B>(B b,B ydd) when ydd.getB().equals("123")-> System.out.println(b);
                case A<B>(B c2,SuperB dcc) when dcc.getB().equals("1234")-> System.out.println("123123123");
                default -> throw new IllegalStateException("Unexpected value: " + o);
            }
        }

        public static void testPattern2(A<B> o){
            switch (o){
                case A<B>(B bb,D ydde)-> System.out.println(ydde);
                case A<B>(B b,B ydd)-> System.out.println(b);
                case A<B>(B c2,SuperB dcc)-> System.out.println("123123123");
            }
        }
    }
}
